package com.nahan.secondapplication.ImitateDouYin;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nahan.secondapplication.App;
import com.nahan.secondapplication.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Desc: 评论与回复列表的适配器
 */

public class CommentExpandAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "CommentExpandAdapter";
    private List<CommentListEntity.RowsBean> commentBeanList;
    private List<CommentListEntity.RowsBean.ReplysBean> replyBeanList;
    private Context context;
    private int pageReply = 1;

    private int totalPageReply;//最大页数




    public CommentExpandAdapter(Context context, List<CommentListEntity.RowsBean> commentBeanList) {
        this.context = context;
        this.commentBeanList = commentBeanList;
    }

    @Override
    public int getGroupCount() {
        return commentBeanList.size();
    }

    @Override
    public int getChildrenCount(int i) {
//        if(commentBeanList.get(i).getReplys() == null){
//            return 0;
//        }else {
//            return commentBeanList.get(i).getReplys().size()<0 ? commentBeanList.get(i).getReplys().size():0;
//        }



        if (commentBeanList.get(i).getCountReply() < 2) {
            return commentBeanList.get(i).getReplys()==null?0:commentBeanList.get(i).getReplys().size();
        } else {
            if (commentBeanList.get(i).isMore()) {
                return commentBeanList.get(i).getReplys().size() + 1;
            } else {
                return 2;
            }
        }

    }

    @Override
    public Object getGroup(int i) {
        return commentBeanList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return commentBeanList.get(i).getReplys().get(i1);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getCombinedChildId(groupPosition, childPosition);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
    boolean isLike = false;

    @Override
    public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) {
        final GroupHolder groupHolder;

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.comment_item_layout_new, viewGroup, false);
            groupHolder = new GroupHolder(convertView);
            convertView.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) convertView.getTag();
        }



        if (commentBeanList.get(groupPosition).getIsAuthor().equals("1"))//是否作者
        {
            groupHolder.comment_zuozhe.setVisibility(View.VISIBLE);
        }else
        {
            groupHolder.comment_zuozhe.setVisibility(View.GONE);
        }


        if (commentBeanList.get(groupPosition).getIsZang()==1)
        {
                GlideUtils.loadImage(App.getInstance(),R.mipmap.ic_yizang_black,groupHolder.iv_like);
                groupHolder.zanshu.setTextColor(context.getResources().getColor(R.color.color_e82b50));

        }else
        {

                GlideUtils.loadImage(App.getInstance(),R.mipmap.ic_weizang_black,groupHolder.iv_like);

            groupHolder.zanshu.setTextColor(context.getResources().getColor(R.color.color_505052));
        }
        groupHolder.zanshu.setText(commentBeanList.get(groupPosition).getCountPraise()+"");
        GlideUtils.loadErrorImage(context,commentBeanList.get(groupPosition).getAvatar(),groupHolder.logo,R.mipmap.ic_launcher);
        groupHolder.tv_name.setText(commentBeanList.get(groupPosition).getUserName());
        groupHolder.tv_time.setText(commentBeanList.get(groupPosition).getCommentTime());


//            groupHolder.tv_name.setTextColor(context.getResources().getColor(R.color.color_8c8b8b));
//            groupHolder.tv_content.setTextColor(context.getResources().getColor(R.color.white));

        SpannableStringBuilder style = new SpannableStringBuilder(commentBeanList.get(groupPosition).getContent()+"\u3000"+commentBeanList.get(groupPosition).getCommentTime());
        style.setSpan(new ForegroundColorSpan(App.getInstance().getResources().getColor(R.color.color_949494)),
                commentBeanList.get(groupPosition).getContent().length(),
                commentBeanList.get(groupPosition).getContent().length()+ 1+commentBeanList.get(groupPosition).getCommentTime().length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        groupHolder.tv_content.setText(style);
        groupHolder.iv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(commentBeanList.get(groupPosition).getIsZang()==1){
//                    isLike = false;
                    cancelPraise(commentBeanList.get(groupPosition).getCommentId(),groupPosition);
//                    groupHolder.iv_like.setColorFilter(Color.parseColor("#aaaaaa"));
                }else {
//                    isLike = true;
                    praise(commentBeanList.get(groupPosition).getCommentId(),groupPosition);
//                    groupHolder.iv_like.setColorFilter(Color.parseColor("#FF5C5C"));
                }
            }
        });


        groupHolder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击头像", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        final ChildHolder childHolder;
        if (commentBeanList.get(groupPosition).getCountReply()<2) {
            if(convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.comment_reply_item_layout_new,viewGroup, false);
                childHolder = new ChildHolder(convertView);
                convertView.setTag(childHolder);
            }
            else {
                childHolder = (ChildHolder) convertView.getTag();
            }
            setData(convertView,childHolder,groupPosition,childPosition,viewGroup);
            return convertView;
        } else {

            if (commentBeanList.get(groupPosition).isMore()) {
                if (childPosition == commentBeanList.get(groupPosition).getReplys().size()) {
                    View v = View.inflate(viewGroup.getContext(), R.layout.item_more, null);
                    LinearLayout ll_gengduo = (LinearLayout) v.findViewById(R.id.ll_gengduo);
                    TextView xian = (TextView) v.findViewById(R.id.xian);
                    TextView tv_gengduo = (TextView) v.findViewById(R.id.tv_gengduo);
                    ImageView jiantou = (ImageView) v.findViewById(R.id.jiantou);
                    xian.setVisibility(View.VISIBLE);
                    jiantou.setRotation(180);
                    tv_gengduo.setText("收起列表");
                    ll_gengduo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            commentBeanList.get(groupPosition).setMore(false);
                            notifyDataSetChanged();

                        }
                    });
                    return v;
                } else {
//                    if(convertView == null){
                        convertView = LayoutInflater.from(context).inflate(R.layout.comment_reply_item_layout_new,viewGroup, false);
                        childHolder = new ChildHolder(convertView);
                        convertView.setTag(childHolder);
//                    }
//                    else {
//                        childHolder = (ChildHolder) convertView.getTag();
//
//                    }
                    setData(convertView,childHolder,groupPosition,childPosition,viewGroup);
                    return convertView;

                }
            } else {
                if (childPosition ==1) {
                    View v = View.inflate(viewGroup.getContext(), R.layout.item_more, null);
                    LinearLayout ll_gengduo = (LinearLayout) v.findViewById(R.id.ll_gengduo);
                    TextView xian = (TextView) v.findViewById(R.id.xian);
                    TextView tv_gengduoa = (TextView) v.findViewById(R.id.tv_gengduo);
                    ImageView jiantou = (ImageView) v.findViewById(R.id.jiantou);
                    xian.setVisibility(View.VISIBLE);

                    tv_gengduoa.setText("展开"+
                            (0==commentBeanList.get(groupPosition).getCountReply()-commentBeanList.get(groupPosition).getReplys().size()?
                                    (commentBeanList.get(groupPosition).getCountReply()-1):
                                    (commentBeanList.get(groupPosition).getCountReply()-commentBeanList.get(groupPosition).getReplys().size())
                                    )
                            +"条回复");
                    ll_gengduo.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

//                            Reply reply=new Reply();
//                            reply.setCommentId(commentBeanList.get(groupPosition).getCommentId());
//                            reply.setPageNum(pageReply);
//                            reply.setPageSize(CommentlistConstant.REPLYPAGESIZE);
//
                            getReply(groupPosition);
                        }
                    });
                    return v;
                } else {
//                    if(convertView == null){
                        convertView = LayoutInflater.from(context).inflate(R.layout.comment_reply_item_layout_new,viewGroup, false);
                        childHolder = new ChildHolder(convertView);
                        convertView.setTag(childHolder);
//                    }
//                    else {
//                        childHolder = (ChildHolder) convertView.getTag();
//                    }
                    setData(convertView,childHolder,groupPosition,childPosition,viewGroup);
                    return convertView;



                }
            }

        }

    }



    /**
     * 子数据
     * @param convertView
     * @param childHolder
     * @param groupPosition
     * @param childPosition
     * @param viewGroup
     */
    private void setData(View convertView, ChildHolder childHolder, int groupPosition, int childPosition, ViewGroup viewGroup) {

        GlideUtils.loadErrorImage(context,commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyUserAvatar(),childHolder.reply_logo,R.mipmap.ic_launcher);
        String replyUser = commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyUserName();
        if(!TextUtils.isEmpty(replyUser)){
            childHolder.tv_name.setText(replyUser);
        }else {
            childHolder.tv_name.setText("无名");
        }

        String repls;

        if (commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyIsAuthor().equals("1"))//作者回复的
        {
            childHolder.reply_zuozhe.setVisibility(View.VISIBLE);
        }else
        {
            childHolder.reply_zuozhe.setVisibility(View.GONE);
        }

//            childHolder.tv_name.setTextColor(context.getResources().getColor(R.color.color_8c8b8b));
//            childHolder.tv_content.setTextColor(context.getResources().getColor(R.color.white));

        if (commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyUserName().contains(commentBeanList.get(groupPosition).getReplys().get(childPosition).getCommentUserName()))
        {
//            childHolder.reply_zuozhe.setVisibility(View.VISIBLE);
            repls= commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyContent()+"\u3000"+commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyTime();
        }else
        {
//            childHolder.reply_zuozhe.setVisibility(View.GONE);
            repls="回复\u3000"+commentBeanList.get(groupPosition).getReplys().get(childPosition).getCommentUserName()+":\u3000"+commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyContent()+"\u3000"+commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyTime();
        }

        SpannableStringBuilder style = new SpannableStringBuilder(repls);
        style.setSpan(new ForegroundColorSpan(App.getInstance().getResources().getColor(R.color.color_949494)),
                repls.length()-commentBeanList.get(groupPosition).getReplys().get(childPosition).getReplyTime().length(),
                repls.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        childHolder.tv_content.setText(style);

        childHolder.reply_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击头像", Toast.LENGTH_SHORT).show();
            }
        });

    }


    /**
     * 获取回复的数据
     * @param groupPosition
     */
    private void getReply(int groupPosition) {


//                        totalPageReply = entity.getData().getTotal() %CommentlistConstant.REPLYPAGESIZE== 0 ? entity.getData().getTotal() /CommentlistConstant.REPLYPAGESIZE : entity.getData().getTotal() / CommentlistConstant.REPLYPAGESIZE + 1;
//
//
//
//                        if (pageReply >= totalPageReply) {
//                            commentBeanList.get(groupPosition).setMore(true);
//                        }else
//                        {
//                            commentBeanList.get(groupPosition).setMore(false);
//                            pageReply++;
//                        }
//                        List<CommentListEntity.RowsBean.ReplysBean> replyBeanList=new ArrayList<>();
//                        for (ReplyListEntity.DataBean.RowsBean rowsBean:entity.getData().getRows()) {
//                            CommentListEntity.DataBean.RowsBean.ReplysBean bean=new Gson().fromJson(new Gson().toJson(rowsBean),CommentListEntity.DataBean.RowsBean.ReplysBean.class);
//                            replyBeanList.add(bean);
//                        }
//                        addReplyList(replyBeanList,groupPosition);

    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupHolder{
        private ImageView logo;
        private TextView tv_name, tv_content, tv_time,zanshu,comment_zuozhe;
        private ImageView iv_like;
        public GroupHolder(View view) {
            logo = (ImageView) view.findViewById(R.id.comment_item_logo);
            tv_content = (TextView) view.findViewById(R.id.comment_item_content);
            tv_name = (TextView) view.findViewById(R.id.comment_item_userName);
            tv_time = (TextView) view.findViewById(R.id.comment_item_time);
            iv_like = (ImageView) view.findViewById(R.id.comment_item_like);
            zanshu = (TextView) view.findViewById(R.id.zanshu);
            comment_zuozhe = (TextView) view.findViewById(R.id.comment_zuozhe);
        }
    }

    private  class ChildHolder{
        private  ImageView reply_logo;
        private TextView tv_name, tv_content,reply_zuozhe;
        public ChildHolder(View view) {
            reply_logo = (ImageView) view.findViewById(R.id.reply_item_logo);
            tv_name = (TextView) view.findViewById(R.id.reply_item_user);
            tv_content = (TextView) view.findViewById(R.id.reply_item_content);
            reply_zuozhe = (TextView) view.findViewById(R.id.reply_zuozhe);
        }
    }


    /**
     * by moos on 2018/04/20
     * func:评论成功后插入一条数据
     * @param commentDetailBean 新的评论数据
     */
    public void addTheCommentData(CommentListEntity.RowsBean commentDetailBean){
        if(commentDetailBean!=null){
            commentBeanList.add(0,commentDetailBean);
           notifyDataSetChanged();
        }else {
            throw new IllegalArgumentException("评论数据为空!");
        }

    }

    /**
     * by moos on 2018/04/20
     * func:回复成功后插入一条数据
     * @param replyDetailBean 新的回复数据
     */
    public void addTheReplyData(CommentListEntity.RowsBean.ReplysBean replyDetailBean, int groupPosition){
        if(replyDetailBean!=null){
            Log.e(TAG, "addTheReplyData: >>>>该刷新回复列表了:"+replyDetailBean.toString() );
            if(commentBeanList.get(groupPosition).getReplys() != null ){
                commentBeanList.get(groupPosition).getReplys().add(replyDetailBean);
            }else {
                List<CommentListEntity.RowsBean.ReplysBean> replyList = new ArrayList<>();
                replyList.add(replyDetailBean);
                commentBeanList.get(groupPosition).setReplys(replyList);
            }
            notifyDataSetChanged();
        }else {
            throw new IllegalArgumentException("回复数据为空!");
        }

    }


    public void addData(List<CommentListEntity.RowsBean> re, CommentExpandableListView expandableListView){

        commentBeanList.addAll(re);
        notifyDataSetChanged();
        for (int i = 0; i < commentBeanList.size(); i++) {
            expandableListView.expandGroup(i);
        }
    }



    /**
     * by moos on 2018/04/20
     * func:添加和展示所有回复
     * @param replyBeanList 所有回复数据
     * @param groupPosition 当前的评论
     */
    private void addReplyList(List<CommentListEntity.RowsBean.ReplysBean> replyBeanList, int groupPosition){
        if(commentBeanList.get(groupPosition).getReplys() != null ){
            commentBeanList.get(groupPosition).getReplys().clear();
            commentBeanList.get(groupPosition).getReplys().addAll(replyBeanList);
        }else {

            commentBeanList.get(groupPosition).setReplys(replyBeanList);
        }

        notifyDataSetChanged();
    }


    /**
     * 点赞
     * @param commentId
     * @param id
     */
    private void praise(int commentId,int id)
    {
                        commentBeanList.get(id).setIsZang(1);
                        commentBeanList.get(id).setCountPraise(commentBeanList.get(id).getCountPraise()+1);
                        notifyDataSetChanged();
    }

    /**
     * 取消点赞
     * @param commentId
     * @param id
     */
    private void cancelPraise( int commentId,int id)
    {
                        commentBeanList.get(id).setIsZang(0);
                        commentBeanList.get(id).setCountPraise(commentBeanList.get(id).getCountPraise()-1);
                        notifyDataSetChanged();
    }


}
